#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFileDialog>
#include <QDir>
#include <QFile>
#include <QMessageBox>
#include <QCloseEvent>

using namespace cv;



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QList<QToolButton *> tbtns = ui->widgetLeft->findChildren<QToolButton *>();
    foreach(QToolButton *btn,tbtns)
           connect(btn,SIGNAL(clicked()),this,SLOT(buttonClick()));


}



void MainWindow::salt(cv::Mat &image, int n)
{
    int i,j;
    for (int k=0; k<n; k++) {

        // rand() is the MFC random number generator
        i= qrand()%image.cols;
        j= qrand()%image.rows;


        if (image.channels() == 1) { // gray-level image

            image.at<uchar>(j,i)= 255;

        } else if (image.channels() == 3) { // color image

            image.at<cv::Vec3b>(j,i)[0]= 255;
            image.at<cv::Vec3b>(j,i)[1]= 255;
            image.at<cv::Vec3b>(j,i)[2]= 255;
        }
    }
}


//void MainWindow::btnIndex(QString &name)
//{
//    QString s;
//    int num = s.mid(1,1);
//    if(name == s)

//}

void MainWindow::buttonClick()
{
    QToolButton *b = (QToolButton *)sender();
//    QString name = b->text();
    QString name = b->objectName();
    QList<QToolButton *> tbtns = ui->widgetLeft->findChildren<QToolButton *>();
    foreach(QToolButton *btn,tbtns){
        if(btn == b)
            btn->setChecked(true);
        else
            btn->setChecked(false);
    }
    //能否编写一个函数，代替if-else语句



    if(name == "_0")
        ui->stackedWidget->setCurrentIndex(0);
    else if(name == "_1")
        ui->stackedWidget->setCurrentIndex(1);
    else if(name == "_2")
        ui->stackedWidget->setCurrentIndex(2);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_sltImg_clicked()
{
//    QString fileName=QFileDialog::getOpenFileName(this,tr("文件对话框"),"C:",tr("图片文件(*png *jpg);;文本文件(*txt)"));
    QString fileName=QFileDialog::getOpenFileName(this,tr("选择图片"),".",tr("图片文件(*.png *.jpg *.jpeg)"));
    if(image1.data){
        image1 = imread(fileName.toStdString());
        namedWindow("原图像");
        imshow("原图像",image1);
    }

}



void MainWindow::on_pushButton_filpImg_clicked()
{
    if(image1.data){
        flip(image1,image1,1);
        namedWindow("旋转图像");
        imshow("旋转图像",image1);
    }

}

void MainWindow::on_pushButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
     tr("Open Image"), ".", tr("Image Files (*.png *.jpg *.bmp)"));

    image= imread(fileName.toStdString());

    if (image.data) {
        namedWindow("Original Image");
        imshow("Original Image", image);
        ui->pushButton_2->setEnabled(true);
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    if(image.data){
        flip(image,image,1); // process the image

         namedWindow("Output Image");
         imshow("Output Image", image);
    }

//    cv::cvtColor(image,image,CV_BGR2RGB);  // change color channel ordering
//    QImage img= QImage((const unsigned char*)(image.data),  // Qt image structure
//                       image.cols,image.rows,QImage::Format_RGB888);
//    ui->label->setPixmap(QPixmap::fromImage(img));  // display on label
//    ui->label->resize(ui->label->pixmap()->size()); // resize the label to fit the image
}
